package MyReflect02;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

public class demo02 {
    public static void main(String[] args) throws IOException, IllegalAccessException {
        Student02 s = new Student02(18,90,"小红",100);
        Teacher02 t = new Teacher02("鲍勃本",23);
        saveObject(t);
    }
    public static void saveObject(Object obj) throws IllegalAccessException, IOException {
        Class clazz = obj.getClass();
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Zdm\\桌面\\MyStudy\\Reflect\\a.txt"));
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            Object value = field.get(obj);
            bw.write(name + "=" + value);
            bw.newLine();
        }
        bw.close();
    }
}
